package com.markerhub.controller;

import com.markerhub.common.lang.Result;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import weibo4j.Log;
import weibo4j.Oauth;
import weibo4j.Users;
import weibo4j.http.AccessToken;
import weibo4j.model.User;
import weibo4j.model.WeiboException;
import weibo4j.util.BareBonesBrowserLaunch;

import java.io.BufferedReader;
import java.sql.SQLOutput;
import java.util.Map;

@RequestMapping("/weibo")
@Component
@Configuration
@RestController
public class WeiboLoginController {

    public String AccessToken;

    public String uid;


    Oauth oauth = new Oauth();

    @GetMapping("/codeUrl")
    public Result getCode() throws WeiboException {


        return Result.succ(oauth.authorize("code"));



    }

    @GetMapping("/callback")
    public Result getAccessToken(@RequestParam("code")String code) throws WeiboException {


       AccessToken token = oauth.getAccessTokenByCode(code);

        System.out.println("Uid是"+token.getUid());
        Users um =new Users(token.getAccessToken());
        User user = um.showUserById(token.getUid());

        System.out.println(user.toString());

        return Result.succ(205,"登陆成功!",user.toString());

    }

}
